﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eBay;

namespace eBayMonitorer {
    public partial class frmFindItemsAdvanced : Form {
        public frmFindItemsAdvanced() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            Dictionary<string, object> ht = new Dictionary<string, object>();
            if (tbBidCountMax.Text.Length > 0) ht["BidCountMax"] = tbBidCountMax.Text;
            if (tbBidCountMin.Text.Length > 0) ht["BidCountMin"] = tbBidCountMin.Text;
            if (tbCategoryHistogramMaxChildren.Text.Length > 0) ht["CategoryHistogramMaxChildren"] = tbCategoryHistogramMaxChildren.Text;
            if (tbCategoryHistogramMaxParents.Text.Length > 0) ht["CategoryHistogramMaxParents"] = label123.Text;
            if (tbCategoryID.Text.Length > 0) ht["CategoryID"] = label1234.Text;
            //if (tbBidCountMax.Text.Length > 0) ht["CategoryIDExclude"] = new List<string>();
            if (tbCharityID.Text.Length > 0) ht["CharityID"] = tbCharityID.Text;
            if (tbCurrency.Text.Length > 0) ht["Currency"] = tbCurrency.Text;
            if (tbFeedbackScoreMax.Text.Length > 0) ht["FeedbackScoreMax"] = tbFeedbackScoreMax.Text;
            if (tbFeedbackScoreMin.Text.Length > 0) ht["FeedbackScoreMin"] = tbFeedbackScoreMin.Text;
            if (tbGroupMaxEntries.Text.Length > 0) ht["GroupMaxEntries"] = tbGroupMaxEntries.Text;
            if (tbGroupsMax.Text.Length > 0) ht["GroupsMax"] = tbGroupsMax.Text;
            if (tbIncludeSelector.Text.Length > 0) ht["IncludeSelector"] = tbIncludeSelector.Text;
            if (tbItemsAvaiableTo.Text.Length > 0) ht["ItemsAvaiableTo"] = tbItemsAvaiableTo.Text;
            if (tbItemsLocatedIn.Text.Length > 0) ht["ItemsLocatedIn"] = tbItemsLocatedIn.Text;
            if (tbMaxDistance.Text.Length > 0) ht["MaxDistance"] = tbMaxDistance.Text;
            if (tbMaxEntries.Text.Length > 0) ht["MaxEntries"] = tbMaxEntries.Text;
            if (tbPageNumber.Text.Length > 0) ht["PageNumber"] = tbPageNumber.Text;
            if (tbPostalCode.Text.Length > 0) ht["PostalCode"] = tbPostalCode.Text;
            if (tbPriceMax.Text.Length > 0) ht["PriceMax"] = tbPriceMax.Text;
            if (tbPriceMin.Text.Length > 0) ht["PriceMin"] = tbPriceMin.Text;
            if (tbProductID.Text.Length > 0) ht["ProductID"] = tbProductID.Text;
            if (tbQuantity.Text.Length > 0) ht["Quantity"] = tbQuantity.Text;
            if (tbQueryKeywords.Text.Length > 0) ht["QueryKeywords"] = tbQueryKeywords.Text;
            if (tbSellerID.Text.Length > 0) ht["SellerID"] = new List<string>();
            //if (tbBidCountMax.Text.Length > 0) ht["SellerIDExclude"] = new List<string>();
            if (tbShippingLocation.Text.Length > 0) ht["ShippingLocation"] = tbShippingLocation.Text;
            if (tbShippingPostalCode.Text.Length > 0) ht["ShippingPostalCode"] = tbShippingPostalCode.Text;
            if (tbStoreName.Text.Length > 0) ht["StoreName"] = tbStoreName.Text;

            ht["EndTimeFrom"] = chbEndTimeFromNull.Checked ? DateTime.MinValue : dtpEndTimeFrom.Value;
            ht["EndTimeTo"] = chbEndTimeToNull.Checked ? DateTime.MinValue : dtpEndTimeTo.Value;
            ht["ModTimeFrom"] = chbModTimeFromNull.Checked ? DateTime.MinValue : dtpModTimeFrom.Value;

            ht["Condition"] = cbCondition.SelectedIndex;
            ht["DescriptionSearch"] = chbDescriptionSearch.Checked;
            ht["ExcludeFlag"] = cbExcludeFlag.SelectedIndex;
            ht["HideDuplicateItems"] = chbHideDuplicateItems.Checked;
            ht["ItemLocationRegion"] = cbItemLocationRegion.SelectedIndex;
            ht["ItemSort"] = cbItemSort.SelectedIndex;
            ht["ItemType"] = cbItemType.SelectedIndex;
            ht["PaymentMethod"] = cbPaymentMethod.SelectedIndex;
            ht["PrefferedLocation"] = cbPrefferedLocation.SelectedIndex;
            ht["QuantityOperator"] = cbQuantityOperator.SelectedIndex;
            ht["SearchFlag"] = cbSearchFlag.SelectedIndex;
            ht["SellerBusinessType"] = cbSellerBusinessType.SelectedIndex;
            ht["SortOrder"] = cbSortOrder.SelectedIndex;
            ht["StoreSearch"] = cbStoreSearch.SelectedIndex;

            FindItemsAdvancedResponseType resp = eBay.API.ShoppingAPI.FindItemsAdvanced(ht);
            if (!resp.HasErrors) {
                StringBuilder rets = new StringBuilder();
                int resNumber = 0;
                foreach (DataRow res in resp.Items) {
                    resNumber++;
                    rets.Append(resNumber + ". " + res["Title"] + " " + res["ItemID"] + "\n");
                }
                MessageBox.Show(rets.ToString());
            }
            else MessageBox.Show("Error happened");
        }
    }
}
